package com.bosssoft.hr.train.springmvc.example.service.impl;

import com.bosssoft.hr.train.springmvc.example.dao.UserDao;
import com.bosssoft.hr.train.springmvc.example.exception.BusinessException;
import com.bosssoft.hr.train.springmvc.example.model.User;
import com.bosssoft.hr.train.springmvc.example.util.result.ResultCode;
import com.bosssoft.hr.train.springmvc.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description 用户业务类
 * @author ChenTong
 * @date 2020/6/5
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**

    @Override
    public List<User> listAll() {
        try {
            return userDao.query();
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.DATA_READ_ERROR,ex);
        }
    }

    @Override
    public boolean insert(User user) {
        if (user==null) return false;
        try {
            return  1!=userDao.save(user);
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.INSERT_FAIL,ex);
        }

    }

    @Override
    public boolean remove(Long id) {
        if (id==null) return false;
        try {
            return (-1 != userDao.remove(id));
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.DEL_FAIL,ex);
        }
    }

    @Override
    public boolean update(User user) {
        if (user==null||user.getId()==null) return false;
        try {
            return (-1 != userDao.update(user));
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.INSERT_FAIL,ex);
        }
    }


    @Override
    public User getUserByName(String name) {
        if (name==null) return null;
        try {
            return userDao.queryUserByName(name);
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.INSERT_FAIL,ex);
        }

    }
     **/

    @Override
    public boolean login(String username, String password) {
        if (username==null||password==null) return false;
        try {
            User user = userDao.queryUserByName(username);
            return user.getPassword().equals(password)&&user.getName().equals(username);
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            throw new BusinessException(ResultCode.PASSWORD_ERROR,ex);
        }
    }
}
